﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public partial class BattleCtr : XObjBase
{
    public static BattleCtr Instance;
    private DataBattleMgr _dataBattleMgr;
    public DataBattleMgr dataBattleMgr
    {
        get
        {
            return _dataBattleMgr;
        }
    }

    protected override void Awake()
    {
        base.Awake();
        Instance = this;
        this.OnInitRender();
    }

    protected override void OnDestroy()
    {
        base.OnDestroy();
        Instance = null;
    }
    protected override void OnEnable()
    {
        base.OnEnable();
        ////强制退出战斗
        //Facade.Instance.RegistNetEvent(nameof(notify_battlefinish), OnHandleNotifyBattlefinishRsp);

        ////有人退出战斗
        Facade.Instance.RegistNetEvent(nameof(notify_exitroom), OnHandleNotifyExitbattleRsp);
        Facade.Instance.RegistNetEvent(nameof(notify_memberoffline), OnHandleNotifyMemberOfflineRsp);
        ////有玩家做操作
        //Facade.Instance.RegistNetEvent(nameof(notify_action), OnHandleNotifyAction);

        //有玩家断网重连进来了
        Facade.Instance.RegistNetEvent(nameof(notify_enterroom), OnHandleNotifyEnterroomRsp);//php
        Facade.Instance.RegistNetEvent(nameof(notify_sycroom), OnHandleNotifySycRoomRsp);//php
        Facade.Instance.RegistNetEvent(nameof(notify_dealcard), OnHandleNotifyDealCard);
        //Facade.Instance.RegistNetEvent(nameof(notify_syncbattle), OnHandleNotifySyncBattleRsp);
        //Facade.Instance.RegistNetEvent(nameof(notify_indicator), OnHandleNotifyIndicator);
        Facade.Instance.RegistNetEvent(nameof(notify_vote), OnHandleNotifyVote);
        Facade.Instance.RegistNetEvent(nameof(notify_voteclear), OnHandleNotifyVoteClear);
        Facade.Instance.RegistNetEvent(nameof(voteclear_rsp), OnHandleVoteClear);
        Facade.Instance.RegistNetEvent(nameof(notify_chat), OnHandleNotifyChatRsp);
    }

    protected override void OnDisable()
    {
        base.OnDisable();
        //Facade.Instance.UnRegistNetEvent(nameof(notify_battlefinish), OnHandleNotifyBattlefinishRsp);
        Facade.Instance.UnRegistNetEvent(nameof(notify_exitroom), OnHandleNotifyExitbattleRsp);//php
        Facade.Instance.UnRegistNetEvent(nameof(notify_memberoffline), OnHandleNotifyMemberOfflineRsp);
        //Facade.Instance.UnRegistNetEvent(nameof(notify_action), OnHandleNotifyAction);//php
        Facade.Instance.UnRegistNetEvent(nameof(notify_enterroom), OnHandleNotifyEnterroomRsp);
        Facade.Instance.UnRegistNetEvent(nameof(notify_sycroom), OnHandleNotifySycRoomRsp);//php
        Facade.Instance.UnRegistNetEvent(nameof(notify_dealcard), OnHandleNotifyDealCard);
        //Facade.Instance.UnRegistNetEvent(nameof(notify_syncbattle), OnHandleNotifySyncBattleRsp);
        //Facade.Instance.UnRegistNetEvent(nameof(notify_indicator), OnHandleNotifyIndicator);
        Facade.Instance.UnRegistNetEvent(nameof(notify_vote), OnHandleNotifyVote);
        Facade.Instance.UnRegistNetEvent(nameof(notify_voteclear), OnHandleNotifyVoteClear);
        Facade.Instance.UnRegistNetEvent(nameof(voteclear_rsp), OnHandleVoteClear);
        Facade.Instance.UnRegistNetEvent(nameof(notify_chat), OnHandleNotifyChatRsp);
    }

    public void OnHandleData(DataBattleMgr data)
    {
        Debug.Log("SceneBattle onahdnle data:" + data);
        _dataBattleMgr = data;
        DataHandle.Instance.dataBattleMgr = null;//防场景初始化完，不要再收消息了
        StartCoroutine(CoWaitUIBattle());
    }

    //数据处理中心
    private void OnHandleNotifyAction(System.Object data)
    {
        //var rsp = data as notify_action;
        //DataBattlePlayer dataBattlePlayer;
        //switch (rsp.type)
        //{
        //    //case ActionType.Close:
        //    //    dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.touid);
        //    //    dataBattlePlayer.SetState(CardType.Close, rsp.fromuid, null);
        //    //    ShowAction(rsp.type, rsp.fromuid, rsp.touid);
        //    //    break;
        //    //case ActionType.Shake:
        //    //    dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.touid);
        //    //    dataBattlePlayer.SetState(CardType.Shake, rsp.fromuid, rsp.content);
        //    //    ShowAction(rsp.type, rsp.fromuid, rsp.touid);
        //    //    break;
        //    //case ActionType.Open:
        //    //    dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.touid);
        //    //    dataBattlePlayer.SetState(CardType.Open, rsp.fromuid, null);
        //    //    ShowAction(rsp.type, rsp.fromuid, rsp.touid);
        //    //    break;
        //    //case ActionType.Glance:
        //    //    dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.touid);
        //    //    dataBattlePlayer.SetState(CardType.Glance, rsp.fromuid, null);
        //    //    ShowAction(rsp.type, rsp.fromuid, rsp.touid);
        //    //    break;
        //    //case ActionType.LookByMySelf:
        //    //    dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.touid);
        //    //    dataBattlePlayer.SetState(CardType.LookByMySelf, rsp.fromuid, null);
        //    //    ShowAction(rsp.type, rsp.fromuid, rsp.touid);
        //    //    break;
        //    case ActionType.Challenge:
        //        dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.fromuid);
        //        dataBattlePlayer.SetChallenge(rsp.content);
        //        break;
        //    case ActionType.Chat:
        //        dataBattlePlayer = _dataBattleMgr.GetDataPlayer(rsp.fromuid);
        //        dataBattlePlayer.SetChat(rsp.content);
        //        break;
        //    default:
        //        Debug.LogError("can not handle type:" + rsp.type);
        //        break;
        //}
        //Facade.Instance.SendEvent(EventID.ActionBattleReceive, rsp);
    }

    private void OnHandleNotifyEnterroomRsp(System.Object data)
    {
        var rsp = data as notify_enterroom;

        var dataPlayer = _dataBattleMgr?.GetDataPlayer(rsp.member.uid);
        if (dataPlayer != null)
        {
            UITips.Instance.Show(XLocalization.Get(rsp.member.name + "重新回来了"));
            dataPlayer.SetCardType((CardType)rsp.member.card_type);
            dataPlayer.SetSupportType((SupportType)rsp.member.support_type);
            dataPlayer.SetRoomState(XLocalization.Get("回来"));
        }
        else
        {
            _dataBattleMgr.AddDataPlayer(rsp.member);
            UITips.Instance.Show(XLocalization.Get(rsp.member.name + "加入房间了"));

            //刷新列表
            StartCoroutine(CoWaitUIBattle());
        }

    }


    private void OnHandleNotifySycRoomRsp(System.Object data)
    {
        var rsp = data as notify_sycroom;

        bool isNew = false;
        foreach (var member in rsp.member_infos)
        {
            var dataPlayer = _dataBattleMgr?.GetDataPlayer(member.uid);
            if (dataPlayer != null)
            {
                dataPlayer.SetCardType((CardType)member.card_type);
                dataPlayer.SetSupportType((SupportType)member.support_type);
            }
            else
            {
                isNew = true;
                _dataBattleMgr.AddDataPlayer(member);
            }
        }

        //刷新列表
        if(isNew)
            StartCoroutine(CoWaitUIBattle());
    }

    private void OnHandleNotifyDealCard(System.Object data)
    {
        //把所有卡牌重置（包括离线的玩家）
        foreach (var member in _dataBattleMgr.userid2data.Values)
        {
            member.SetCardType(CardType.Invalid);
            member.SetSupportType(SupportType.Invalid);
        }

        //设置在线的卡牌
        var rsp = data as notify_dealcard;
        var member_infos = rsp.member_infos;
        foreach (var item in member_infos)
        {
            if (item.uid != DataPlayer.Instance.userId)
                continue;
            //先设置自己
            var dataPlayer = _dataBattleMgr?.GetDataPlayer(item.uid);
            if (dataPlayer != null)
            {
                dataPlayer.SetCardType((CardType)item.card_type);
                dataPlayer.SetSupportType((SupportType)item.support_type);
                if (dataPlayer.GetUserId() == rsp.driver_uid)
                    dataPlayer.SetIsDriver(true);
            }
            break;
        }

        foreach (var item in member_infos)
        {
            if (item.uid == DataPlayer.Instance.userId)
                continue;
            //后设置别人
            var dataPlayer = _dataBattleMgr?.GetDataPlayer(item.uid);
            if (dataPlayer != null)
            {
                dataPlayer.SetCardType((CardType)item.card_type);
                dataPlayer.SetSupportType((SupportType)item.support_type);
                if (dataPlayer.GetUserId() == rsp.driver_uid)
                    dataPlayer.SetIsDriver(true);
            }
        }
    }

    private void OnHandleNotifyVote(System.Object data)
    {
        var rsp = data as notify_vote;
        var dataPlayer = _dataBattleMgr?.GetDataPlayer(rsp.uid);
        if (dataPlayer != null)
        {
            dataPlayer.SetSupportType((SupportType)rsp.support_type);
        }
    }

    private void OnHandleNotifyVoteClear(System.Object data)
    {
        //把所有卡牌重置（包括离线的玩家）
        foreach (var member in _dataBattleMgr.userid2data.Values)
        {
            member.SetSupportType(SupportType.Invalid);
        }

        var rsp = data as notify_voteclear;
        foreach (var item in rsp.simplemember_infos)
        {
            var dataPlayer = _dataBattleMgr?.GetDataPlayer(item.uid);
            if (dataPlayer != null)
            {
                dataPlayer.SetSupportType((SupportType)item.support_type);
            }
        }

    }

    //private void OnHandleNotifySyncBattleRsp(System.Object data)
    //{
    //    var rsp = data as notify_syncbattle;

    //    var persons = rsp.persons;
    //    foreach (var person in persons)
    //    {
    //        var dataBattlePlayer = _dataBattleMgr.GetDataPlayer(person.uid);
    //        dataBattlePlayer.SetState((CardType)person.state, person.uid, person.content);
    //    }

    //    //统计骰子
    //    Facade.Instance.SendEvent(EventID.CardTypeChange, null);
    //}

    //private void OnHandleNotifyIndicator(System.Object data)
    //{
    //    var rsp = data as notify_indicator;
    //    //是否轮到我喊骰子
    //    _dataBattleMgr.ChallengeUid = rsp.uid;
    //}

    //private void OnHandleNotifyBattlefinishRsp(System.Object data)
    //{
    //    var rsp = data as notify_battlefinish;
    //    UITips.Instance.Show(XLocalization.Get("服务器错误，强制退出"));
    //    SceneMgr.Instance.LoadScene(SceneName.SceneMain);
    //}

    private void OnHandleNotifyExitbattleRsp(System.Object data)
    {
        var rsp = data as notify_exitroom;

        var dataPlayer = _dataBattleMgr.GetDataPlayer(rsp.uid);
        if (dataPlayer != null)
        {
            dataPlayer.SetRoomState(XLocalization.Get("退出"));
        }
    }

    private void OnHandleNotifyMemberOfflineRsp(System.Object data)
    {
        var rsp = data as notify_memberoffline;

        var dataPlayer = _dataBattleMgr.GetDataPlayer(rsp.uid);
        if (dataPlayer != null)
        {
            dataPlayer.SetRoomState(XLocalization.Get("离线"));
        }
    }

    private void OnHandleVoteClear(System.Object data)
    {
        var rsp = data as voteclear_rsp;
        if (rsp.ret != NetErrorCode.None)
            UITips.Instance.Show("出错了:" + rsp.reason);
    }

    private void OnHandleNotifyChatRsp(System.Object data)
    {
        var rsp = data as notify_chat;
        var dataPlayer = _dataBattleMgr.GetDataPlayer(rsp.uid);
        if (dataPlayer != null)
        {
            dataPlayer.SetChat(rsp.msg);
        }
    }
}